home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / src / mactech / volume14_1998 / 14.12.sit / 14.12 / OpenGL Part One.c < prev   
C/C++ Source or Header  |  2006-09-22  |  4KB  |  167 lines

  1. /* Rotating cube with color interpolation */
  2.  
  3. /* This program demonstrates the features of OpenGL */
  4. /* discussed in the Mactech article: OpenGL for Mac Users: Part 1 */
  5.  
  6. /* For one button mouse use mouse, control-mouse, shift-mouse */
  7. /* to choose between increment x, y or z angles */
  8.  
  9. /* q or Q key will terminate program */
  10. /* 1 and 2 keys toggle between rotation and no rotation */
  11. /* 3 and 4 keys toggle between opaque and translucent surfaces  */
  12. /* 5 and 6 keys toggle between smooth and falt shading */
  13. /* 7 and 8 keys toggle hidden surface removal */
  14.  
  15. #include <stdlib.h>
  16. #include "glut.h"
  17.  
  18.  
  19.     GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
  20.     {-1.0,1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, 
  21.     {1.0,-1.0,1.0}, {-1.0,1.0,1.0}, {1.0,1.0,1.0}};
  22.  
  23.     GLfloat colors[][4] = {{0.0,0.0,0.0,0.5},{1.0,0.0,0.0,0.5},
  24.     {0.0,1.0,0.0,0.5}, {1.0,1.0,0.0,0.5}, {0.0,0.0,1.0,0.5}, 
  25.     {1.0,0.0,1.0,0.5}, {0.0,1.0,1.0,0.5}, {1.0,1.0,1.0,0.5}};
  26.  
  27. void quad(int a, int b, int c , int d)
  28. {
  29.  
  30. /* draw a polygon via list of vertices */
  31. /* assign a color to each vertex */
  32.  
  33.      glBegin(GL_QUADS);
  34.         glColor4fv(colors[a]);
  35.         glVertex3fv(vertices[a]);
  36.         glColor4fv(colors[b]);
  37.         glVertex3fv(vertices[b]);
  38.         glColor4fv(colors[c]);
  39.         glVertex3fv(vertices[c]);
  40.         glColor4fv(colors[d]);
  41.         glVertex3fv(vertices[d]);
  42.     glEnd();
  43.                                                                                                         }
  44.  
  45. void cube(void)
  46. {
  47.  
  48. /* map vertices to faces */
  49.  
  50.     quad(3,1,0,2);
  51.     quad(2,6,7,3);
  52.     quad(6,2,0,4);
  53.     quad(1,3,7,5);
  54.     quad(4,5,7,6);
  55.     quad(5,4,0,1);
  56. }
  57.  
  58. static GLfloat theta[] = {0.0,0.0,0.0};
  59. static GLint axis = 2;
  60.  
  61. void display(void)
  62. {
  63. /* display callback, clear frame buffer and z buffer,
  64.    rotate cube and draw, swap buffers */
  65.  
  66.  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67.     glLoadIdentity();
  68.     glRotatef(theta[0], 1.0, 0.0, 0.0);
  69.     glRotatef(theta[1], 0.0, 1.0, 0.0);
  70.     glRotatef(theta[2], 0.0, 0.0, 1.0);
  71.  
  72.     cube();
  73.  
  74.     glutSwapBuffers();
  75. }
  76.  
  77. /* Idle callback*/
  78.  
  79. void spinCube()
  80. {
  81.  
  82. /* spin cube 2 degrees about selected axis */
  83.  
  84.     theta[axis] += 2.0;
  85.     if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
  86.     glutPostRedisplay();        /* request redisplay */
  87. }
  88.  
  89. /* mouse callback, selects an axis about which to rotate */
  90.  
  91. void mouse(int btn, int state, int x, int y)
  92. {
  93.     if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;    /* x axis */
  94.     if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;    /* y axis */
  95.     if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;    /* z axis */
  96. }
  97.  
  98. /* keyboard callback */
  99.  
  100. void mykey(unsigned char key, int i, int j)
  101. {
  102.     if(key=='q'||key=='Q') exit(0);            /* Terminate program */
  103.     if(key=='1') glutIdleFunc(NULL);        /* Turn off rotation */
  104.     if(key=='2') glutIdleFunc(spinCube);    /* Start rotation */
  105.     if(key=='3')                             /* Diable blending */
  106.     {
  107.         glDisable(GL_BLEND); 
  108.         glutPostRedisplay();
  109.     }
  110.     if(key=='4')                             /* Enable blending */
  111.     {
  112.         glEnable(GL_BLEND); 
  113.         glutPostRedisplay();
  114.     }
  115.     if(key=='5')                             /* Smooth shading */
  116.     {
  117.           glShadeModel(GL_SMOOTH); 
  118.         glutPostRedisplay();
  119.     }
  120.     if(key=='6')                             /* Flat shading */
  121.     {
  122.         glShadeModel(GL_FLAT); 
  123.         glutPostRedisplay();
  124.     }
  125.     if(key=='7')                             /* Hidden Surface Removal on */
  126.     {
  127.         glEnable(GL_DEPTH_TEST);
  128.         glutPostRedisplay();
  129.     }
  130.     if(key=='8')                             /* Hidden Surface Removal off */
  131.     {
  132.         glDisable(GL_DEPTH_TEST);
  133.         glutPostRedisplay();
  134.     }
  135. }
  136.  
  137. /* Default user settings */
  138.  
  139. void myinit()
  140. {
  141.     glClearColor(1.0, 1.0, 1.0, 1.0);                /* Clear to white */
  142.     glMatrixMode(GL_PROJECTION);                    /* Set clipping window */
  143.     glLoadIdentity();
  144.     glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);        /* 2 x 2 x 2 cube */
  145.     glMatrixMode(GL_MODELVIEW);                        /* Return to ModelView mode */
  146.     glShadeModel(GL_SMOOTH);                        /* Smooth Shading */
  147.     glEnable(GL_BLEND);                                /* Blending On */
  148.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); /* Blend parameters */
  149. }
  150.  
  151.  
  152. void
  153. main(int argc, char **argv)
  154. {
  155.     glutInit(&argc, argv);            /* Initialize GLUT */
  156.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  157.                                     /* RGB color, double buffering and depth buffer */
  158.     glutInitWindowSize(200, 200);    /* Request 200 x 200 window */
  159.     glutCreateWindow("Cube Demo");    /* Window title */
  160.     glutDisplayFunc(display);        /* Display callback */
  161.     glutIdleFunc(NULL);                /* Idle callback */
  162.     glutMouseFunc(mouse);            /* Mouse callback */
  163.     glutKeyboardFunc(mykey);        /* Keyboard callback */
  164.     myinit();                        /* User settings */
  165.     glutMainLoop();                    /* Enter event loop */
  166. }
  167.